package com.base.cn.platform.os.controller.planTools;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.planTools.PopupSlogan;
import com.base.cn.platform.os.entity.planTools.PopupSloganCondition;
import com.base.cn.platform.os.service.planTools.PopupSloganBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Map;


/**
 * 弹窗广告
 *
 * @Author: lxr
 * @Date: 2020/06/30 14:43
 */
@RestController
@RequestMapping("/manage")
public class PopupSloganController extends BaseController {

    @Autowired
    private PopupSloganBiz popupSloganBiz;

    /**
     * 分页查询弹窗广告
     *
     * @param condition
     * @param currentPage
     * @return
     */
    @RequestMapping("/findPopupSloganListPage")
    public PageInfo<PopupSlogan> findPopupSloganListPage(@RequestBody PopupSloganCondition condition,
                                                         @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage) {
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        return popupSloganBiz.findPopupSloganListPage(condition, page);
    }

    /**
     * 保存弹窗广告
     *
     * @param popupSlogan
     * @return
     */
    @RequestMapping("/savePopupSlogan")
    public Map<String, Object> saveCourseOpen(@RequestBody PopupSlogan popupSlogan) {
        if (StringUtils.isEmpty(popupSlogan.getName())) {
            return ResultUtil.ERROR("请输入弹窗广告名称");
        }
        if (popupSlogan.getName().trim().length() > 10) {
            return ResultUtil.ERROR("免费直播名称不能超过10个字符");
        }
        if (StringUtils.isEmpty(popupSlogan.getImag())) {
            return ResultUtil.ERROR("请上传弹窗广告图片");
        }
        if (StringUtils.isEmpty(popupSlogan.getChainedType())) {
            return ResultUtil.ERROR("请选择链接类型");
        } else {
            if (!"0".equals(popupSlogan.getChainedType())) {
                if (StringUtils.isEmpty(popupSlogan.getChained())) {
                    return ResultUtil.ERROR("请输入链接地址");
                }
            }
        }
        if (ObjectUtils.isEmpty(popupSlogan.getStartTime())) {
            return ResultUtil.ERROR("请指定弹窗广告开始时间");
        }
        if (ObjectUtils.isEmpty(popupSlogan.getEndTime())) {
            return ResultUtil.ERROR("请指定弹窗广告结束时间");
        }
        if (!popupSlogan.getStartTime().before(popupSlogan.getEndTime())) {
            return ResultUtil.ERROR("弹窗广告开始时间不能等于或大于结束时间");
        }
        if (ObjectUtils.isEmpty(popupSlogan.getPutDitchPc()) && ObjectUtils.isEmpty(popupSlogan.getPutDitchMobile())) {
            return ResultUtil.ERROR("请指定投放渠道");
        }
        if (ObjectUtils.isEmpty(popupSlogan.getPutDitchPc())) {
            popupSlogan.setPutDitchPc(0);
        }
        if (ObjectUtils.isEmpty(popupSlogan.getPutDitchMobile())) {
            popupSlogan.setPutDitchMobile(0);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        StringBuffer whereSql = new StringBuffer("status = 1");
        if(popupSlogan.getPutDitchPc().intValue() == 1){
             whereSql.append(" and putDitchPc =1");
            if (DataUtil.idIsNotNull(popupSlogan.getId())) {
                whereSql.append(" and id !=" +popupSlogan.getId());
            }
        }
        if(popupSlogan.getPutDitchMobile().intValue() == 1){
            whereSql.append(" and putDitchMobile =1");
            if (DataUtil.idIsNotNull(popupSlogan.getId())) {
                whereSql.append(" and id !=" +popupSlogan.getId());
            }
        }
        whereSql.append(" and (('"+sdf.format(popupSlogan.getStartTime())+"' BETWEEN startTime AND endTime) or ('"+sdf.format(popupSlogan.getEndTime())+"' BETWEEN startTime AND endTime))");
        PopupSlogan popupSlogan_have = popupSloganBiz.findOne(whereSql.toString(),null);
        if(ObjectUtils.isNotEmpty(popupSlogan_have)){
            return ResultUtil.ERROR("该时间段已存在活动弹框");
        }
        if (popupSlogan.getAppearType().intValue() == 2) {
            if (StringUtils.isEmpty(popupSlogan.getAppearData())) {
                return ResultUtil.ERROR("请输入间隔时间");
            } else {
                if (popupSlogan.getAppearData().contains(".") || popupSlogan.getAppearData().contains("-")) {
                    return ResultUtil.ERROR("间隔时间需为正整数");
                }
            }
        }
        popupSlogan = popupSloganBiz.savePopupSlogan(popupSlogan);
        return ResultUtil.SUCCESS("弹窗广告信息保存成功", popupSlogan);
    }

    /**
     * 修改弹窗广告状态
     *
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("/updatePopupSloganStatus")
    public Map<String, Object> updatePopupSloganStatus(@RequestParam("id") String id,
                                                       @RequestParam("status") int status) {
        popupSloganBiz.updatePopupSloganStatus(id, status);
        return ResultUtil.SUCCESS("状态修改成功");
    }

    /**
     * 查询弹窗广告
     *
     * @param id
     * @return
     */
    @RequestMapping("/findPopupSloganById")
    public PopupSlogan findCourseOpenById(@RequestParam("id") BigDecimal id) {
        return popupSloganBiz.findPopupSloganById(id);
    }

    /**
     * 获取是否有弹框
     * @param type
     * @param userId
     * @return
     */
    @RequestMapping("/getIndexPopupSlogan")
    public Map<String, Object> getIndexPopupSlogan(@RequestParam("type") String type,@RequestParam("userId") String userId) {
        return popupSloganBiz.getIndexPopupSlogan(type,userId);
    }
}
